package com.pjj.boardservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjj.boardservice.Entity.Board;
import com.pjj.boardservice.mapper.BoardMapper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 76004
 * @apiNote
 * @date 2023/2/15 12:12
 */
@Service
public class BoardService {
    @Autowired
    private BoardMapper boardMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public Object insertBoard(Board board){
        Date date = new Date();
        board.setTime(new Timestamp(date.getTime()));
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","添加成功");
        boardMapper.insert(board);
        redisTemplate.opsForValue().increment("board");
        return map;
    }

    public Object deleteBoardById(int boardId){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","删除成功");
        boardMapper.deleteById(boardId);
        redisTemplate.opsForValue().decrement("board");
        return map;
    }

    public Object updateBoard(Board board){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","更改成功");
        boardMapper.updateById(board);
        return map;
    }

    public Object selectById(int boardId){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        map.put("data",boardMapper.selectById(boardId));
        return map;
    }

    public Object selectPage(int pageNum,int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        map.put("data",boardMapper.selectPage(new Page<Board>(pageNum,pageSize), new QueryWrapper<Board>()).getRecords());
        return map;
    }

    public Object selectBoardCount(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        map.put("data",redisTemplate.opsForValue().get("board"));
        return map;
    }
}
